<template>
  <div class="layout">
    <!-- 搜索顶部 -->
    <div class="search-head">
      <span>搜索</span>
    </div>

    <!-- 搜索框 -->
    <div class="search-box">
      <img src="@/assets/img/ui/search2.png" alt="" />
      <input type="text" placeholder="艺人、歌曲或播客" @click="showSearch" />
    </div>

    <!-- 搜索查找页 -->
    <div class="search-detail" v-show="isSearch">
      <!-- 关闭搜索窗口 -->
      <div class="search-close" v-if="!isInput" @click="hideSearch">
        <img src="@/assets/img/ui/arrow_left.png" alt="" />
      </div>

      <!-- 搜索 -->
      <div class="search" ref="search">
        <img src="@/assets/img/ui/search2.png" alt="" />
        <input
          type="text"
          placeholder="艺人、歌曲或播客"
          v-model="searchVal"
          @input="showClose(), showData(), resetInput()"
          @keydown.enter="getSearch"
        />
      </div>

      <!-- 搜索结果 -->
      <div class="res-box">
        <app-search class="res"></app-search>
      </div>
    </div>

    <!-- 分类 -->
    <div class="sort-box">
	  <app-load v-show="!isLoad"></app-load>
      <ul class="sort">
        <li
          v-for="(item, index) in search.sortData"
          :key="index"
          :style="`background-color:${bgColor[index]}`"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import appSearch from "@/components/app-search.vue";
import appLoad from "@/components/app-load.vue";
import { mapState } from "vuex";
export default {
  name: "search",
  components: {
    appSearch,
	appLoad
  },
  data() {
    return {
      searchVal: "", // 输入框数据
      isSearch: false, // 判断是否点击搜索框
      isInput: false, // 判断是否输入值
	  isLoad: false, // 判断页面是否加载完毕
      bgColor: [
        "#FF80B0",
        "#9399FF",
        "#21BF73",
        "#FF677D",
        "#3282B8",
        "#FA163F",
        "#FD2EB3",
        "#FDD365",
        "#FB8D62",
        "#CC0066",
        "#9400d3",
        "#12CAD6",
        "#52DE97",
        "#FD5E53",
        "#FF80B0",
        "#9399FF",
        "#21BF73",
        "#FF677D",
        "#3282B8",
        "#FA163F",
        "#FD2EB3",
        "#FDD365",
        "#FB8D62",
        "#CC0066",
        "#9400d3",
        "#12CAD6",
        "#52DE97",
        "#FD5E53",
        "#FF80B0",
        "#9399FF",
        "#21BF73",
        "#FF677D",
        "#3282B8",
        "#FA163F",
        "#FD2EB3",
        "#FDD365",
        "#FB8D62",
        "#CC0066",
        "#9400d3",
        "#12CAD6",
        "#52DE97",
        "#FD5E53",
        "#FF80B0",
        "#9399FF",
        "#21BF73",
        "#FF677D",
        "#3282B8",
        "#FA163F",
        "#FD2EB3",
        "#FDD365",
        "#FB8D62",
        "#CC0066",
        "#9400d3",
        "#12CAD6",
        "#52DE97",
        "#FD5E53",
        "#FF80B0",
        "#9399FF",
        "#21BF73",
        "#FF677D",
        "#3282B8",
        "#FA163F",
        "#FD2EB3",
        "#FDD365",
        "#FB8D62",
        "#CC0066",
        "#9400d3",
        "#12CAD6",
      ], // 背景色数组
    };
  },
  computed: {
    ...mapState(["search"]),
  },
  methods: {
    // 获取搜索结果
    getSearch() {
      axios
        .get("/search", { params: { keywords: this.searchVal, limit: 50 } })
        .then((data) => {
          let arr = [];
          if (data.code == 200) {
            data.result.songs.forEach((item) => {
              let obj = {};
              obj.name = item.name;
              obj.id = item.id;
              obj.artists = item.artists[0].name;
              arr.push(obj);
            });
            let isNull = false;
            // 传值到vuex的actions中
            this.$store.dispatch("search/setIsNullAsync", isNull);
            this.$store.dispatch("search/setSearchResAsync", arr);
          }
        });
    },
    // 获取歌单分类数据
    getSort() {
      axios.get("/playlist/catlist").then((data) => {
        let arr = [];
        // console.log(data);
        if (data.code == 200) {
          data.sub.forEach((item) => {
            let obj = {};
            obj.name = item.name;
            arr.push(obj);
          });
		  this.isLoad = true;
          // 传值到vuex的actions中
          this.$store.dispatch("search/setSortDataAsync", arr);
        }
      });
    },
    // 显示搜索页
    showSearch() {
      this.isSearch = true;
    },
    // 隐藏搜索页
    hideSearch() {
      this.isSearch = false;
    },
    showClose() {
      if (this.searchVal.length != 0) {
        // 隐藏关闭按钮
        this.isInput = true;
        this.$nextTick(() => {
          if (this.$refs["search"]) {
            this.$refs["search"].style["margin-left"] = "15px";
          }
        });
      } else {
        // 显示关闭按钮
        this.isInput = false;
        this.$nextTick(() => {
          if (this.$refs["search"]) {
            this.$refs["search"].style["margin-left"] = "50px";
          }
        });
      }
    },
    // 显示搜索结果
    showData() {
      let isNull = "";
      if (this.searchVal.length == 0) {
        isNull = true;
      }
      // 传值到vuex的mutations中
      this.$store.dispatch("search/setIsNullAsync", isNull);
    },
    // 清空输入框重置搜索内容
    resetInput() {
      if (this.searchVal.length == 0) {
        let arr = [];
        this.$store.dispatch("search/setSearchResAsync", arr);
      }
    },
  },
  created() {
    this.getSort();
    this.showData();
  },
};
</script>

<style lang="scss" scoped>
.layout {
  border: 1px solid transparent;
  color: white;
  background-color: #121212;
  // 搜索顶部
  .search-head {
    height: 30px;
    line-height: 30px;
    margin: 50px 15px 0;
    span {
      font-size: 20px;
      font-weight: 600;
    }
  }
  // 搜索框
  .search-box {
    margin: 0 15px;
    padding-top: 10px;
    box-sizing: border-box;
    box-shadow: 0 0 10px 2px black;
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    background-color: #121212;
    position: sticky;
    top: 0;
    img {
      width: 20px;
      height: 20px;
      position: absolute;
      left: 12px;
      top: 18px;
    }
    input {
      width: 100%;
      height: 38px;
      border-radius: 5px;
      padding-left: 38px;
      box-sizing: border-box;
      font-size: 16px;
      &::placeholder {
        color: #121212;
      }
    }
  }
  // 搜索页
  .search-detail {
    width: 100%;
    height: 100%;
    background-color: #121212;
    position: fixed;
    top: 0;
    left: 0;
    overflow: hidden;
    // 关闭按钮
    .search-close {
      width: 28px;
      height: 28px;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      top: 30px;
      left: 15px;
      img {
        width: 68%;
      }
    }
    // 搜索框
    .search {
      margin: 15px 15px 0 50px;
      padding: 10px 0;
      box-sizing: border-box;
      position: relative;
      transition: margin-left 0.3s;
      img {
        width: 20px;
        height: 20px;
        position: absolute;
        left: 12px;
        top: 18px;
      }
      input {
        width: 100%;
        height: 38px;
        border-radius: 5px;
        padding-left: 38px;
        box-sizing: border-box;
        font-size: 16px;
        &::placeholder {
          color: #121212;
        }
      }
    }
    // 搜索结果
    .res-box {
      height: 100%;
      overflow-y: scroll;
    }
  }
  // 分类区域
  .sort-box {
    margin-bottom: 60px;
    margin-top: 35px;
    padding-left: 15px;
    box-sizing: border-box;
    .sort {
      display: grid;
      grid-template-columns: 1fr 1fr;
      li {
        height: 80px;
        line-height: 80px;
        text-align: center;
        border-radius: 8px;
        margin-right: 15px;
        margin-bottom: 15px;
      }
    }
  }
}
</style>